using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Utility;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI.SmartParts;
using System.Windows.Forms.Integration;
using Microsoft.Practices.CompositeUI.WinForms;

namespace SCSFContrib.CompositeUI.Visualizer
{
    [System.ComponentModel.ToolboxItem(false)]
    public partial class WpfVisualizer : UserControl
    {
        public WpfVisualizer()
        {
            InitializeComponent();
        }

        [InjectionConstructor]
        public WpfVisualizer(IVisualizer visualizer, WorkItem workItem)
            : this()
        {
            LoadViewer(visualizer.CabRootWorkItem);

            ShowSmartPart(workItem);
        }

        private void ShowSmartPart(WorkItem workItem)
        {
            ISmartPartInfo smartPartInfo = new SmartPartInfo("WorkItems", String.Empty);

            if (workItem.Workspaces["MainWorkspace"] is TabWorkspace)
                workItem.Workspaces["MainWorkspace"].Show(this, smartPartInfo);
            else
            {
                // Pure WPF Layer

                WindowsFormsHost host = new WindowsFormsHost();
                host.Child = this;

                workItem.Workspaces["MainWorkspace"].Show(host, smartPartInfo);
            }
        }

        private void LoadViewer(WorkItem workItem)
        {
            ElementHost elementHost = new ElementHost();
            elementHost.Dock = DockStyle.Fill;    

            Viewer viewerUserControl = new Viewer(workItem);
            viewerUserControl.OnSelectedItem += delegate(object sender, DataEventArgs e)
            {
                if (!this.IsDisposed)
                    this.propertyGrid.SelectedObject = e.Data;
            };

            elementHost.Child = viewerUserControl;
            elementHost.Show();

            this.splitContainer1.Panel1.Controls.Add(elementHost);
            this.Refresh();
        }
    }
}